package com.hans.myscanner.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.Switch;
import android.widget.TextView;

import com.hans.myscanner.R;
//自定义字体
public class CustomFontTextView extends TextView{
    private Context context;
    private String fontPath;

    public CustomFontTextView(Context context) {
        this(context,null);
    }

    public CustomFontTextView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CustomFontTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context=context;
        getAttrs(attrs);
        setCustomFontInAssets(fontPath);
    }

    private void getAttrs(AttributeSet attrs) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomFontTextView);
        fontPath = ta.getString(R.styleable.CustomFontTextView_fontPathAssets);
        ta.recycle();//回收
    }

    private void setCustomFontInAssets(String fontPath){
        try {
            if(fontPath!=null) setTypeface(Typeface.createFromAsset(context.getAssets(),fontPath));
          //  if(fontPath != null) setTypeface(Typeface.createFromAsset(context.getAssets(),fontPath));
        } catch (Exception e){
            Log.e("Hans","你配置的字体路径有误，在Assets目录下找不到字体:"+fontPath);
            e.printStackTrace();
        }

    }
}
